<?php
namespace app\common\service;

use app\common\util\Util;
use think\Db;
use think\Model;
use think\Cache;

class AdminPublicService extends BaseService
{
    /**
     * @param $username 登陆用户名
     * @param $password 登陆密码
     * 后台登陆
     */
    public function adminLogin($username, $password)
    {

        $username = Util::tel_encode($username);
        $where = [
            'username' => $username,
            'password' => trim(md5($password))
        ];

        return $this->loginLogic($where);
    }


    /**
     * 登录逻辑
     * @param array $where
     * @return array
     * @throws \think\Exception
     */
    public function loginLogic($where = [])
    {
        $admin = db('store_admin')
            ->where($where)
            ->find();
        if (!$admin) {
            return ['status' => 0, 'msg' => '用户名或密码错误'];
        }

        if (!$admin['status']) {
            return ['status' => 0, 'msg' => '此管理账户被删除或禁用'];
        }

        $store = db('store')->where(['store_id' => $admin['sid']])->find();

        if (!$store) {
            return ['status' => 0, 'msg' => '未找到管理店铺'];
        }
        if ($store['end_time'] < time()) {
            return ['status' => 0, 'msg' => '店铺试用期已过，请联系管理员！'];
        }
        if (!$store['status']) {
            return ['status' => 0, 'msg' => '店铺被删除或禁用！'];
        }

        $updateData = [
            'lastlogintime' => time(),
            'lastloginip' => $this->getIp()
        ];

        db('store_admin')->where(['aid' => $admin['aid']])->update($updateData);

        // 插入Token值
        $auth_token = md5(microtime().rand(0,999999));
        $token_data = [
            'auth_token' => $auth_token,
            'sid' => $admin['sid'],
            'add_time' => time(),
            'status' => 1
        ];
        db('login_token')->insert($token_data);

        Cache::set('store_admin', $admin);
        Cache::set('store_auth', $store);

        $admin['username'] = Util::tel_decode($admin['username']);
        $admin['store_name'] = $store['store_name'];
        $admin['store_domain'] = $store['domain'];
        $admin['store_logo'] = $store['logo'];
        $admin['store_logo_small'] = $store['logo_small'];
        $admin['store_description'] = $store['description'];
        $admin['end_time'] = date('Y年m月d日', $store['end_time']);
        $admin['auth_token'] = $auth_token;

        $info['content'] = $admin;
        $data = ['status' => 1, 'data' => $info, 'msg' => '登陆成功'];
        return $data;
    }

    //获取用户真实IP
    private function getIp()
    {
        $onlineip = '';
        if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $onlineip = getenv('HTTP_CLIENT_IP');
        } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $onlineip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $onlineip = getenv('REMOTE_ADDR');
        } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $onlineip = $_SERVER['REMOTE_ADDR'];
        }
        return $onlineip;
    }

}